//
//  GKBookCaseDataQueue.swift
//  MySwiftObject
//
//  Created by wangws1990 on 2019/9/9.
//  Copyright © 2019 wangws1990. All rights reserved.
//

import UIKit
let tableName = "bookCase";
let primaryId = "_id";

class GKBookCaseDataQueue: NSObject {
    class func insertBookModel(bookDetail:GKBookDetailModel,sucesss:@escaping ((_ success : Bool) ->Void)){
        BaseDataQueue.insertData(toDataBase: tableName, primaryId: primaryId, userInfo: bookDetail.toJSON()!) { (res) in
            if res{
                GKHomeNetManager.reloadHomeData(options: GKLoadOptions.Database);
            }
            sucesss(res);
        }
    }
    class func deleteBookModel(bookId:String,sucesss:@escaping ((_ success : Bool) ->Void)){
        BaseDataQueue.deleteData(toDataBase: tableName, primaryId: primaryId, primaryValue: bookId) { (res) in
            if res{
                GKHomeNetManager.reloadHomeData(options: GKLoadOptions.Database);
            }
            sucesss(res);
        }
    }
    class func getBookModel(bookId:String,sucesss:@escaping ((_ bookModel : GKBookDetailModel) ->Void)){
        BaseDataQueue.getDataFromDataBase(tableName, primaryId: primaryId, primaryValue: bookId) { (object) in
            let model : GKBookDetailModel = GKBookDetailModel.deserialize(from: (object as NSDictionary))!
            sucesss(model);
        }
    }
    class func getBookModels(page :Int,size: Int,sucesss:@escaping ((_ listData : NSArray) ->Void)){
        BaseDataQueue.getDatasFromDataBase(tableName, primaryId: primaryId, page: page, pageSize: size) { (object) in
            var arrayDatas :[GKBookDetailModel] = [];
            let datas:NSArray = object as NSArray;
            for obj in datas{
                let model : GKBookDetailModel = GKBookDetailModel.deserialize(from: (obj as! NSDictionary))!
                arrayDatas.append(model);
            }
            sucesss(arrayDatas as NSArray);
        }
    }
    class func getBookModels(sucesss:@escaping ((_ listData : NSArray) ->Void)){
        BaseDataQueue.getDatasFromDataBase(tableName, primaryId: primaryId) { (object) in
            var arrayDatas :[GKBookDetailModel] = [];
            let datas:NSArray = object as NSArray;
            for obj in datas{
                let model : GKBookDetailModel = GKBookDetailModel.deserialize(from: (obj as! NSDictionary))!
                arrayDatas.append(model);
            }
            sucesss(arrayDatas as NSArray);
        };
    }
}
